<?php
namespace app\product\controller;
use think\View;
use think\Controller;
use think\Db;
use think\Model;

class Product extends Controller
{

    public function test_count(){
        $user = Db::name('user');
        $b = $user->where('utime','')->select();
        $a = $user->count();
        print_r($b);
    }


//赋值
    public function index()
    {
        $data['res'] = db('user')->select();
        $view = new View();

//        多个赋值 使用数组方式
        $data['name'] = 'ThinkPHP';
        $data['email'] = 'thinkphp@qq.com';
        $view->assign('data', $data);

//        多个赋值 使用assign
//        $data1['name'] = 'is_data1';
//        $view->assign('data1',$data1);

        return $this->fetch('index');
//        return view('index');
    }


    //数据库添加
    function insert_user()
    {
        //判断是否post进来
        if (!request()->isPost()) {
            return view('insert_user');
        } else {
            $data = request()->post();
            $result = $this->validate(
                $data,
                [
                    'name'  => 'require|unique:user',
                    'confirmpwd'=>'require|confirm:pwd',
                    'remark'   =>  'require|max:5|number',
                ]);
            if(true !== $result){
                // 验证失败 输出错误信息
                $this->error($result);
            }else{
                $res = db('user')->insert($data);
                if($res){
                   $this->success('添加成功','product/Product/index');
                }else{
                    $this->error('添加失败');
                }
            }
        }
    }

    function edit_detail(){
        if(request()->isPost()){
            $data['name'] = input('post.name');
            $data['remark'] = input('post.remark');
            $data['utime'] = time();
            $res = db('user')->where('id',input('post.id'))->update($data);
            if($res){
                $data['msg'] = 1;
            }else{
                $data['msg'] = 0;
            }
            return json($data);
        }
    }

    function edit_user($id)
    {
        //判断是否post进来
        if (!request()->isPost()) {
            $view = new View();
            $res = db('user')->where('id', $id)->find();
            $view->assign('u', $res);
            return $view->fetch();
        } else {
            $user = model('User');
            $data['name'] = input('post.name');
            $data['remark'] = input('post.remark');
            $user->where('id',$id)->update($data);
        }
    }

//    function edit_user($id)
//    {
//
//        //判断是否post进来
//        if (!request()->isPost()) {
//            $view = new View();
//            $res = db('user')->where('id', $id)->find();
//            $view->assign('u', $res);
//            return $view->fetch();
//        } else {
//
//            $data['remark'] = input('post.remark');
//            $data['utime'] = time();
//            $res = db('user')->where('id', $id)->update($data);
//            if ($res) {
//                $this->success('编辑成功', 'product/product/index');
//            } else {
//                $this->error('编辑失败');
//            }
//        }
//    }

    function del_user($id)
    {
        $res = db('user')->where('id',$id)->delete();
        if($res){
            $this->success('删除成功','product/Product/index');
        }else{
            $this->error('删除失败');
        }
    }


    //查询的方式
    function table()
    {

//        $res = db('user')->where('id', 'neq', 1)->where('name', 'like', '%a%')->select();

//        多条件查询

//        $res = db('user')->where([
//            'id'=>['neq',1],
//            'name'=>['neq','bao']
//        ])->select();


//        $res = Db::View('user','id,name')
//            ->View('group','group.id = group_user.group_id')
//            ->View()
//            ->select();

////        join(多表查询)
//        $res = Db::table('user')->field('a.name as name,a.remark as remark,b.name as group_name,b.remark as group_remark')->alias('a')
//            ->join('group_user c','a.id = c.user_id')
//            ->join('group b','c.group_id = b.id')
//            ->select();


        //类似getField(a,true)
        $user = db('user')->select();
        $res = Db::table('group_user')->where('group_id',1)->column('user_id');
        foreach($user as &$u){
            if(is_array($res)){
                $u['group_1'] = in_array($u['id'],$res)?1:0;
            }
         }
        $this->assign('user',$user);
        return $this->fetch();
    }


    //测试1 模板输出方式
    function demo1()
    {

        $res = db()->select();
////        没有赋值的时候
//        return view('demo1');
////        或者
//        return view();
//
//
////        有赋值的2种方式(建议用方式2)
//        //方式1
//        $view = new View();
//        $view->assign('res', $res);
//        return $view->fetch('index');

        //方式2
        $this->assign('res', $res);
        return $this->fetch();


    }
}